/**
 * Auto gen via dbpojo-gen.
 */
package com.us.usws.service.ro.impl;

import java.util.ArrayList;
import java.util.List;

import net.iwsh.core.model.bean.NameValueBean;
import net.iwsh.core.model.bean.impl.NameValuePair;

import org.apache.commons.lang3.StringUtils;

import com.us.usws.biz.ResourceURIReplacer;
import com.us.usws.cache.BaseDictCache;
import com.us.usws.cache.UserCache;
import com.us.usws.common.SiteConstants;
import com.us.usws.dao.SchoolDAO;
import com.us.usws.dao.VideoFileDAO;
import com.us.usws.model.domain.base.BaseDict;
import com.us.usws.model.domain.system.School;
import com.us.usws.model.domain.video.VideoFile;
import com.us.usws.service.ro.SchoolROService;
import com.us.usws.support.ext.CacheManager;
import com.us.usws.support.uitls.DataPageUtil;

/**
 * Service RO impl class
 * @author robot
 */
public class SchoolROServiceImpl implements SchoolROService {

	/**
	 * DAO instance
	 */
	private SchoolDAO dao = new SchoolDAO();
	
	private VideoFileDAO videoFiledao = new VideoFileDAO();

	/**
	 * Get Count.
	 */
	@Override
	public int getTotalCount() {
		return dao.getTotalCount();
	}

	/**
	 * Get all list.
	 */
	@Override
	public List<School> getAllSchools() {
		return dao.getAllSchools();
	}
	
	public void loadSchoolsByPage(DataPageUtil<School> dataPageUtil) {
		dao.loadSchoolsByPage(dataPageUtil);
		List<School> rs = dataPageUtil.getResult();
		BaseDictCache dictCache = CacheManager.getDictCache();
		UserCache userCache = CacheManager.getUserCache();
		if(rs != null && rs.size() > 0) {
			for(School s:rs) {
				
				s.setProvinceName(dictCache.getDictNameByCode(SiteConstants.DICT_DOMAIN_REGION, s.getProvinceCode()));
				s.setCityName(dictCache.getDictNameByCode(SiteConstants.DICT_DOMAIN_REGION, s.getCityCode()));
				s.setDistrictName(dictCache.getDictNameByCode(SiteConstants.DICT_DOMAIN_REGION, s.getStrictCode()));
				s.setCreateUserName(userCache.getUserName(s.getCreateByUid()));
				s.setSchoolTypeName(dictCache.getDictNameBySepId(SiteConstants.DICT_DOMAIN_SCHOOL_TYPE, s.getSchoolType()));
				
				String eduStage = s.getEduStage();
				if(StringUtils.isNotBlank(eduStage)) {
					String[] eduStages = StringUtils.split(eduStage, ",");
					String st = "";
					int len = eduStages.length;
					Integer[] eduS = new Integer[len];
					for(int i=0; i<len; i++) {
						eduS[i] = Integer.parseInt(eduStages[i].trim());
						if(i != len - 1) {
							st += dictCache.getDictNameBySepId(SiteConstants.DICT_DOMAIN_EDU_STAGE, eduS[i]) + ",";
						} else {
							st += dictCache.getDictNameBySepId(SiteConstants.DICT_DOMAIN_EDU_STAGE, eduS[i]);
						}
					}
					s.setStages(eduS);
					s.setSchoolEduStages(st);
				}
				
				VideoFile vf = videoFiledao.getVideoFile(s.getVideoFileUid());
				if(vf != null) {
					s.setVideoSnapshotURL(ResourceURIReplacer.getVideoSnapshotURL(vf.getUid(), vf.getSnapshot()));
					s.setVideoFileURL(ResourceURIReplacer.getVideoFileURL(vf.getUid(), vf.getFileExt()));
				}
			}
		}
	}
	
	public List<NameValueBean> loadSchoolEudStage(String uid) {
		List<NameValueBean> nvbs = new ArrayList<NameValueBean>();
		if(StringUtils.isNotBlank(uid)) {
			School s = getSchool(uid);
			BaseDictCache dictCache = CacheManager.getDictCache();
			String eduStage = s.getEduStage();
			if(StringUtils.isNotBlank(eduStage)) {
				String[] eduStages = StringUtils.split(eduStage, ",");
				int len = eduStages.length;
				for(int i=0; i<len; i++) {
				   BaseDict db = dictCache.getBaseDictBySepId(SiteConstants.DICT_DOMAIN_EDU_STAGE, Integer.parseInt(eduStages[i].trim()));
				   if(db != null) {
					   nvbs.add(new NameValuePair(db.getName(), db.getCode()));
				   }
				}
			}
		}
		return nvbs;
	}

	/**
	 * Get all list by pagination.
	 */
	@Override
	public List<School> getSchools(int start, int end) {
		return dao.getSchools(start, end);
	}

	/**
	 * Get School by primary key.
	 */
	@Override
	public School getSchool(String uid) {
		School s = dao.getSchool(uid);
		return s;
	}

}